package com.hwadeefsc.springboot.service.impl;

import com.hwadeefsc.springboot.dao.MessagesDao;
import com.hwadeefsc.springboot.entity.Messages;
import com.hwadeefsc.springboot.service.MessagesService;
import com.hwadeefsc.springboot.utils.DateUtil;
import com.hwadeefsc.springboot.vo.MessagesUserVo;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;
import java.util.List;

/**
 * (Messages)表服务实现类
 *
 * @author makejava
 * @since 2024-07-09 21:43:27
 */
@Service("messagesService")
public class MessagesServiceImpl implements MessagesService {
    @Resource
    private MessagesDao messagesDao;

    /**
     * 通过ID查询单条数据
     *
     * @param messageId 主键
     * @return 实例对象
     */
    @Override
    public Messages queryById(Integer messageId) {
        return this.messagesDao.queryById(messageId);
    }

    /**
     * 分页查询
     *
     * @param messages 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<Messages> queryByPage(Messages messages, PageRequest pageRequest) {
        long total = this.messagesDao.count(messages);
        return new PageImpl<>(this.messagesDao.queryAllByLimit(messages, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param messages 实例对象
     * @return 实例对象
     */
    @Override
    public Messages insert(Messages messages) {
        messages.setCreatedAt(DateUtil.getStringDate());
        this.messagesDao.insert(messages);

        return messages;
    }

    /**
     * 修改数据
     *
     * @param messages 实例对象
     * @return 实例对象
     */
    @Override
    public Messages update(Messages messages) {
        this.messagesDao.update(messages);
        return this.queryById(messages.getMessageId());
    }

    /**
     * 通过主键删除数据
     *
     * @param messageId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer messageId) {
        return this.messagesDao.deleteById(messageId) > 0;
    }

    @Override
    public List<MessagesUserVo> getAllMessagesWithDetails(Messages messages) {
        return messagesDao.selectMessagesWithUserDetails(messages);
    }

    @Override
    public List<MessagesUserVo> queryALL() {
        return messagesDao.queryAll();
    }
}
